package com.yanftch.hdc_tools.insertcode.util;

import com.yanftch.hdc_tools.insertcode.bean.CodeBean;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;

public class FileUtils {
    private static final String path = "files/code_default.json";

    public static final List<CodeBean> getList() {
        return parseJsonToList(readJsonFile(path));
    }

    private static List<CodeBean> parseJsonToList(String jsonContent) {
        return GsonUtils.INSTANCE.jsonToList(jsonContent, CodeBean.class );
    }

    private static String readJsonFile(String filePath) {
        InputStream inputStream = FileUtils.class.getClassLoader().getResourceAsStream(filePath);
        if (inputStream == null) {
            throw new RuntimeException("File not found: " + filePath);
        }

        try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
            return scanner.useDelimiter("\\A").next();  // 读取整个文件为字符串
        }
    }
}
